/**
 * User: alesj
 * Date: 2004.8.11
 * Time: 15:09:32
 */

package com.generalynx.ecos.data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.alesj.newsfeed.core.MsgCodeException;
import com.generalynx.common.constants.WebConst;
import com.generalynx.common.data.ADEData;
import com.generalynx.common.data.IVersion;
import com.generalynx.common.resources.Resources;
import com.generalynx.common.userdata.IModuleUser;
import com.generalynx.common.web.module.Module;
import com.generalynx.ecos.data.dao.IBasicDAO;
import com.generalynx.ecos.utils.Context;
import org.hibernate.LockMode;
import org.springframework.beans.factory.BeanFactory;

/**
 * @hibernate.class table="euser" proxy="com.generalynx.ecos.data.User"
 */
public class User extends ADEData implements IModuleUser, IVersion, IAttachable, ICreatorComponentAware, Serializable {

    private static final long serialVersionUID = 1L;

    private int m_id;
    private Integer m_version;
    private Person m_person;
    private Address m_address;
    private String m_username;
    private String m_password;
    private CreatorComponent m_creator;
    private Integer m_defaultModule;
    private Set m_roles;
    private Set m_modules;

    private boolean dirty;
    private boolean reloaded;

    //------------------------------------------------

    public boolean hasPermission(Module module) {
        return getModules().contains(module);
    }

    public boolean isDirty() {
        return dirty;
    }

    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    public boolean check() {
        return true;
    }

    public Serializable getKey() {
        return new Integer(getId());
    }

    public boolean isStaleObjectState() {
        return false;
    }

    public LockMode getLockMode() {
        return LockMode.NONE;
    }

    public boolean doReload() {
        return isDirty();
    }

    public boolean isReloaded() {
        return reloaded;
    }

    public void setReloaded(boolean reloaded) {
        this.reloaded = reloaded;
    }

    public String getAddLabel() {
        return Resources.getString(Resources.USER);
    }

    protected String getNameLabel() {
        if (getPerson() != null) {
            return getPerson().toString();
        }
        return getUsername();
    }

    protected String[] getSelectActions() {
        return new String[]{"userRoleSelect", "userModuleSelect"};
    }

    public void beforeBind() {
        setAddress(new Address());
        setPerson(new Person());
    }

    public void beforeInsert(Context context) {
        setCreator(new CreatorComponent());
        User creator = (User)context.getAttribute(WebConst.USER);
        getCreator().setCreatorId(creator.getId());
        getCreator().setCreationTime(new Date());
    }

    public void beforeSaveOrUpdate(BeanFactory beanFactory) {
        IBasicDAO basicDAO = (IBasicDAO)beanFactory.getBean(IBasicDAO.basicDAO);
        List<User> users = basicDAO.findEntity("from User where username = ?", getUsername());
        if (!users.isEmpty() && users.get(0).getId() != getId()) {
            throw new MsgCodeException("user_exists");
        }
    }

    //------------------------------------------------

    /**
     * @hibernate.id  column="euser_id" unsaved-value="0" generator-class="native"
     */
    public int getId() {
        return m_id;
    }

    public void setId(int id) {
        m_id = id;
    }

    /**
     * @hibernate.version column="uversion" type="integer" unsaved-value="null"
     */
    public Integer getVersion() {
        return m_version;
    }

    public void setVersion(Integer version) {
        m_version = version;
    }

    /**
     * @hibernate.component class="com.generalynx.ecos.data.Person"
     */
    public Person getPerson() {
        return m_person;
    }

    public void setPerson(Person person) {
        m_person = person;
    }

    /**
     * @hibernate.component class="com.generalynx.ecos.data.Address"
     */
    public Address getAddress() {
        return m_address;
    }

    public void setAddress(Address address) {
        m_address = address;
    }

    /**
     * @hibernate.property
     */
    public String getUsername() {
        return m_username;
    }

    public void setUsername(String username) {
        m_username = username;
    }

    /**
     * @hibernate.property
     */
    public String getPassword() {
        return m_password;
    }

    public void setPassword(String password) {
        m_password = password;
    }

    /**
     * @hibernate.component class="com.generalynx.ecos.data.CreatorComponent"
     */
    public CreatorComponent getCreator() {
        return m_creator;
    }

    public void setCreator(CreatorComponent creator) {
        m_creator = creator;
    }

    /**
     * @hibernate.property column="def_module"
     */
    public Integer getDefaultModule() {
        return m_defaultModule;
    }

    public void setDefaultModule(Integer defaultModule) {
        m_defaultModule = defaultModule;
    }

    /**
     * @hibernate.set name="roles" table="eusererole" cascade="none" lazy="true"
     * @hibernate.collection-key column="euser_id"
     * @hibernate.collection-many-to-many class="com.generalynx.ecos.data.Role" column="erole_id"
     */
    public Set getRoles() {
        return m_roles;
    }

    public void setRoles(Set roles) {
        m_roles = roles;
    }

    /**
     * @hibernate.set name="modules" inverse="false" table="moduleuser" lazy="false" cascade="none"
     * @hibernate.cache usage="read-write"
     * @hibernate.collection-key column="euser_id"
     * @hibernate.collection-many-to-many column="modul_id" class="com.generalynx.common.web.module.Module"
     */
    public Set getModules() {
        return m_modules;
    }

    public void setModules(Set modules) {
        m_modules = modules;
    }

}
